<?php
include_once 'MY/extra_sql.php';
function GetID($prefix) {
	//第一步:初始化种子
	//microtime(); 是个数组
	$seedstr =split(" ",microtime(),5);
	$seed =$seedstr[0]*10000;
	//第二步:使用种子初始化随机数发生器
	srand($seed);
	//第三步:生成指定范围内的随机数
	$random =rand(1000,10000);

	$filename = date("Ymd", time()).$random.$prefix;

	return $filename;
}
class TreeController extends Zend_Controller_Action{
	
	function init(){
		if($_SESSION['user']==""){
			 echo "<script>window.location='../user/login';</script>"; exit;
		}

		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->template = "tree";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
		
		$group_id = $_SESSION['user']['group_id'];
		$group = new Group();
		$group = $group->getgroupByid($group_id);
		$groups = explode(',',$group[0]['models']);
		if(!in_array('tag',$groups)){
			echo "<script>alert('No Permission To Enter!');history.go('-1');</script>";exit();
		}
	}

	public function indexAction(){


	}

	//添加标签树方法
	public function addAction(){
		$filter=new Zend_Filter_StripTags();
		$tree = new Tree();
        $operation = new Operation();
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$summary=$this->_request->getPost('summary');
			$key=$filter->filter(($this->_request->getPost('key')));
            $status=$filter->filter(($this->_request->getPost('status')));
		    $category=$filter->filter(($this->_request->getPost('category')));

			$create_time = time();
			$data = array(
				'name'=>$name,
				'summary'=>$summary,
				'key_word'=>$key,
				'user_id'=>$_SESSION['user']['user_id'],
				'status'=>$status,
				'category'=>$category,
				'create_time'=>$create_time,
			);

			$tree_id = $tree->insert($data);
			if($tree_id){
				$time = time();
				$data = array(
					'op_name'=>'add',
					'model_name'=>'tree',
					'user_id'=>$_SESSION['user']['user_id'],
					'user_name'=>$_SESSION['user']['user_name'],
					'name'=>'添加标签树',
					'create_time'=>$time
				);
				$query = $operation->insert($data);
				echo "<script>window.location='detail?tree_id=".$tree_id."'</script>";
			}else{
				echo "<script>alert('Failure!');window.location='detail?tree_id=".$tree_id."'</script></script>";
			}
		}
		echo $this->view->render('tree/add.html');
	}

	//修改标签树方法
  	public function editAction(){
        $filter=new Zend_Filter_StripTags();
		$tree = new Tree();
		$user = new User();
		$operation = new Operation();
		$tree_id=$filter->filter(($this->_request->getParam('tree_id')));
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$name=$filter->filter(($this->_request->getPost('name')));
			$summary=$this->_request->getPost('summary');
			$key=$filter->filter(($this->_request->getPost('key')));
			$status=$filter->filter(($this->_request->getPost('status')));
			$category=$filter->filter(($this->_request->getPost('category')));

			$create_time = time();
			$data = array(
				'name'=>$name,
				'summary'=>$summary,
				'key_word'=>$key,
				'user_id'=>$_SESSION['user']['user_id'],
				'status'=>$status,
				'category'=>$category,
				'create_time'=>$create_time,
			);
                      //  print_r($data);exit;
			$query = $tree->updatetreeBytree_id($data, $tree_id);
			if($query){
				$time = time();
				$data = array(
					'op_name'=>'edit',
					'model_name'=>'tree',
					'user_id'=>$_SESSION['user']['user_id'],
					'user_name'=>$_SESSION['user']['user_name'],
					'name'=>'更新标签树',
					'create_time'=>$time
				);
				$query = $operation->insert($data);
				echo "<script>window.location='detail?tree_id=".$tree_id."'</script>";
			}else{
				echo "<script>alert('Failure!');window.location='detail?tree_id=".$tree_id."'</script>";
			}
		}else{
			$this->view->tree = $tree->gettreeBytree_id($tree_id);

			$this_tree = $this->view->tree;
			$this->view->user = $user->getuserByuser_id($this_tree[0]['user_id']);
		}

		echo $this->view->render('tree/edit.html');
	}


	//标签树列表页
	public function listAction(){
		$filter=new Zend_Filter_StripTags();
		$tree = new Tree();
        $operation = new Operation();
		$page=$filter->filter(($this->_request->getParam('page')));
		$search_name=$filter->filter(($this->_request->getParam('search_name')));
        $search_key=$filter->filter(($this->_request->getParam('search_key')));
		if($page==""){
			$page="1";
		}
		$count = $tree->getAllTrees('','', $search_name,$search_key);
//总条数
		$total = count($count);
//总页数
		$perpage = "10";
		$pages = ceil($total/$perpage);

		$this->view->trees = $tree->getAllTrees($page, $perpage, $search_name,$search_key);
               // print_r($this->view->trees);
		$this->view->page = $page;
		$this->view->pages = $pages;
		$this->view->total = $total;
		$time = time();
		$data = array(
			'op_name'=>'list',
			'model_name'=>'tree',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'查看所有标签树',
			'create_time'=>$time
		);
		$query = $operation->insert($data);

		echo $this->view->render('tree/list.html');
	}

	//标签树详情页
	public function detailAction(){
        $filter=new Zend_Filter_StripTags();
		$tree = new Tree();
		$user = new User();
		$tag = new Tag();
		$project = new Project();
		$projecttag = new ProjectTag();
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		$operation = new Operation();
		$tree_id=$filter->filter(($this->_request->getParam('tree_id')));
		$tag_id=$filter->filter(($this->_request->getParam('tag_id')));
		$ps_page=$filter->filter(($this->_request->getParam('ps_page')));
	    if ($ps_page=="") {
			$ps_page="1";
		}

		$this->view->tree = $tree->gettreeBytree_id($tree_id);
		$this_tree = $this->view->tree;
		$this->view->user = $user->getuserByuser_id($this_tree[0]['user_id']);
		$this->view->tags = $db->gettagtree($tree_id);
		$this->view->alltag = $tag->getAllTags('','','','');
		$this->view->alltree = $tree->getAllTrees('','','','');

//tag 相关信息
		if ($tag_id) {
			$this->view->tag = $tag->gettagBytag_id($tag_id);
			$projecttags = $projecttag->getprojecttagBytag_id($tag_id,9999,'');
			$arr = array('0'=>'0');
			for($i=0;$i<count($projecttags);$i++){
				$arr[].=$projecttags[$i]['project_id'];
			}
			//标签相关项目
			$project = new Project();
			$projecttag = new ProjectTag();
			$new_tag = $tag->gettagBytag_id($tag_id);
			$this->view->tag = $new_tag;
			$key_word = $new_tag[0]['key_word'];
			$arrProjectTags = $project->getProjectRelated(array($key_word),9999,'');
			$ps_total = count($arrProjectTags);
	
			//总页数
			$perpage = "10";
			$ps_pages = ceil($ps_total/$perpage);
			$ps_start = ($ps_page-1)*$perpage;
			$ps_end = $perpage;
			$this->view->ps_page = $ps_page;
			$this->view->ps_pages = $ps_pages;
			$this->view->ps_total = $ps_total;
			$arrProjectTags = $project->getProjectRelated(array($key_word),$ps_page,$ps_end);
			foreach ($arrProjectTags as $row) {
				$arrProject = $project->getprojectByproject_id($row['project_id']);
				$arrProjects[] = $arrProject[0];
			}
			$this->view->projects = $arrProjects;
			
			//标签相关学生
			$student = new Student();
			$studenttag = new StudentTag();
			$arrStudentTags = $studenttag->getstudenttagBytag_id($tag_id,9999,'');
			foreach ($arrStudentTags as $row) {
				$arrStudent = $student->getstudentBystudent_id($row['student_id']);
				$arrStudents[] = $arrStudent[0];
			}
			$this->view->students = $arrStudents;
		}         
		$time = time();
		$data = array(
			'op_name'=>'detail',
			'model_name'=>'tree',
			'user_id'=>$_SESSION['user']['user_id'],
			'user_name'=>$_SESSION['user']['user_name'],
			'name'=>'查看标签树详情',
			'create_time'=>$time
		);
		$query = $operation->insert($data);
		echo $this->view->render('tree/detail.html');
	}

	//删除标签树
    public function deleteAction(){
        $filter=new Zend_Filter_StripTags();
		$tree = new Tree();
		$operation = new Operation();
		$tree_id=$filter->filter(($this->_request->getParam('tree_id')));

		$query = $tree->deltreeBytree_id($tree_id);

		if($query){
			$time = time();
			$data = array(
				'op_name'=>'delete',
				'model_name'=>'tree',
				'user_id'=>$_SESSION['user']['user_id'],
				'user_name'=>$_SESSION['user']['user_name'],
				'name'=>'删除标签树',
				'create_time'=>$time
			);
			$query = $operation->insert($data);
			echo "<script>window.location='list'</script>";
		}else{
			 
		}
	}

	//标签树管理方法
	public function manageAction(){
		$filter=new Zend_Filter_StripTags();
		$tag = new Tag();
		$tree = new Tree();
		$tagtree = new TagTree();
		$tag_id = $filter->filter(($this->_request->getParam('tag_id')));
		$tree_id = $filter->filter(($this->_request->getParam('tree_id')));
		$search = $filter->filter(($this->_request->getParam('search')));
		$db = new mysql(constant('dbhost'),constant('dbuser'),constant('dbpass'),constant('dbname'),"UTF8");
		if (empty($tag_id)) $tag_id = 1;
		$this->view->tags =$db->gettagtree($tree_id, 0);
		$this->view->tag = $tag->gettagBytag_id($tag_id);
		$this->view->tagtree = $tagtree->gettagtreeBytree_id($tree_id);
		$this->view->alltag = $tag->getAllTags('','', $search,'');
		$this->view->alltree = $tree->getAllTrees('','','','');
		echo $this->view->render('tree/manage.html');
	}

	public function addtagtreeAction(){
		$filter=new Zend_Filter_StripTags();
		$tagtree = new TagTree();
		
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$tag_id=$filter->filter(($this->_request->getPost('tag_id')));
			$tree_id=$filter->filter(($this->_request->getPost('tree_id')));
			$pid=$filter->filter(($this->_request->getPost('pid')));
			
			$data = array(
				'tag_id'=>$tag_id,
				'tree_id'=>$tree_id,
				'pid'=>$pid
			);
			$query = $tagtree->insert($data);
			if($query){
				echo "<script>window.location='manage?tree_id=".$tree_id."'</script>";
			}else{
				echo "<script>alert('Failure!');window.location='manage?tree_id=".$tree_id."'</script>";
			}
		}
	}

	public function deletetagtreeAction(){
		$filter=new Zend_Filter_StripTags();
		$tagtree = new TagTree();
		$id = $filter->filter(($this->_request->getParam('id')));
		$tree_id = $filter->filter(($this->_request->getParam('tree_id')));
		$tag_id = $filter->filter(($this->_request->getParam('tag_id')));
		
		$query = $tagtree->deltagtreeByid($id,$tag_id);
		if($query){
			echo "<script>window.location='manage?tree_id=".$tree_id."'</script>";
		}else{
			echo "<script>alert('Failure!');window.location='manage?tree_id=".$tree_id."'</script>";
		}
	}

	public function fileAction(){
		$filter=new Zend_Filter_StripTags();
		$tagfile = new TagFile();
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
			$tag_tree_id=$filter->filter(($this->_request->getPost('tag_tree_id')));
			$tree_id=$filter->filter(($this->_request->getPost('tree_id')));
			$summary=$this->_request->getPost('summary');
			
			$path= "public/files/tree/".date("Ym",time())."/";//上传路径
			if(!file_exists($path)){
				mkdir("$path", 0700);
			}//END IF
			$path= "public/files/tree/".date("Ym",time())."/".date("d",time())."/";//上传路径
			if(!file_exists($path)){
				mkdir("$path", 0700);
			}
			$name=$_FILES["file_upload"]["name"];
			$file1=GetID( ".".strtolower(substr($_FILES['file_upload']['name'], (strrpos($_FILES['file_upload']['name'], '.') + 1))));
			$path = $path.$file1;
			move_uploaded_file($_FILES["file_upload"]["tmp_name"],$path);
			$time = time();
			$array = array(
				'tag_tree_id'=>$tag_tree_id,
				'summary'=>$summary,
				'file_name'=>$name,
				'path'=>$path,
				'create_time'=>$time,
				'status'=>'0'
			);
			$id = $tagfile->insert($array);
			if($id){
				echo "<script>alert('Success');window.location='manage?tree_id=".$tree_id."'</script>";
			}else{
				echo "<script>alert('Failure');window.location='manage?tree_id=".$tree_id."'</script>";
			}
		}
	}
}
?>
